<?php
/**
 * @page custom_fields_setup Custom Fields Setup
 * 
 * This is the most important section of this documentation, and should be read carefully to understand
 * the concept of listing types, fieldsets, fields and how its linked to a category.
 * 
 * @section fieldsets Field Groups
 * 
 * Field groups / fieldsets is a group of fields tied together which can be used across your different 
 * type of listings. A field group can belong to either a 'Review' or 'Listing' entity. A good example 
 * of this would be a field group, say, 'location'. So a typical 'location' field group can contain 
 * fields like 'address', 'city', 'suburb' linked to listing entity. Similarly a field group, 'service'
 * can have fields 'speed of service', 'treatment', 'cleanliness' etc.
 * 
 * @section listing_type Listing Type
 * 
 * It defines how a listing and its review should behave. That is, what fields will be shown while adding 
 * a review or a listing. A listing type can contain multiple field groups. So, lets say I create a new
 * listing type called 'hotel', and link it to field groups of 'location' (the one we defined in previous
 * section) and 'service'. Now our 'hotel' listing type can show our desired fields on "Add Listing" page
 * or "Add review" page. All you need to do after creating few listing types is linking them to categories.
 * 
 * You can also define rateable fields in a listing type like shown here:
 * @image html rateable_fields.jpg
 * 
 * Weightage can be used to specify weight of a particular paramater. This will affect in overall rating.
 * For example - A hotel may have 2 rateable fields 'service' and 'ambience', we can define 'service' to 
 * have double weight than 'ambience' by writing '2' in weightage field of service and '1' next to ambience.
 * 
 * So, if we get 2 hotel listings which ratings:
 * 
 * Hotel 1:
 * Service: 4
 * Ambience: 3
 * 
 * Hotel 2:
 * Service: 3
 * Ambience: 4
 * 
 * Then, Hotel 1 would score better.
 *
 */
class ListingType extends AppModel
{
	var $name = 'ListingType';
	var $validate = array('name'=>
							array(
								'required'=>true,
								'rule'=>VALID_NOT_EMPTY,
								'message'=>'Please enter listing type name.')
					);
						
	var $hasMany = array(
					'RateableField'=>array(
							'className'=>'RateableField',
							'foreignKey'=>'listing_type_id',
							'dependent'=>true,
							)
					);
					
	var $hasAndBelongsToMany = array(
								'Fieldset'=>array(
										'className'=>"Eav.Fieldset",
										'with'=>"ListingTypeFieldset",'order'=>"field_order ASC")
								);
	var $fieldProperties = array(
		'id'=>array(
			'type'=>'hidden',
			'group'=>array('edit')
		),
		'name'=>array(
			'type'=>'text',
			'group'=>array('add','edit'),
			'tooltip' => 'Name of your listing type'
		),
		
		'rateable_fields' => array(
			'type' => 'parent', 
			'group' => array('add'),
			'childInfo' => array(
				'model' => 'RateableField', 
				'child_group' => 'listing_type_add',
				'allowMultiple' => true
			)
		),
		'Fieldset'=>array(
			'label'=>'Field Group',
			'group'=>array('add','edit'),
			'class'=>'select_fieldgroups',
			'tooltip' => 'Associate your listing type with necessary fieldsets.Fields present inside selected fieldsets will show up while adding a new review or listing.'
		)
		
	);
	
	function getCustomFields($id,$entity = null)
	{
		$fieldsetIds = $this->ListingTypeFieldset->find('list',array('conditions'=>array('listing_type_id'=>$id),'fields'=>array('fieldset_id'),'order'=>"field_order ASC"));
		if($entity !=null && $fieldsetIds)
		{
			$fieldsetIds = $this->Fieldset->find('all',array('conditions'=>array('id'=>$fieldsetIds,'entity'=>$entity),'order'=>"FIELD(id,".implode(',',$fieldsetIds).")",'recursive'=>-1));
		}
		$fields = array();
        $order  = 1;
		foreach($fieldsetIds as $fieldset)
		{
			$fieldsetFields = $this->Fieldset->getFieldsetFieldProperties($fieldset['Fieldset']['id']);
			foreach($fieldsetFields as $field=>$fieldInfo)
			{
				$fieldsetFields[$field]['fieldset_id'] = $fieldset['Fieldset']['id'];
				$fieldsetFields[$field]['fieldset_name'] = $fieldset['Fieldset']['name'];
				$fieldsetFields[$field]['fieldset_label'] = $fieldset['Fieldset']['label'];
				$fieldsetFields[$field]['fieldset_show_label'] = $fieldset['Fieldset']['show_label'];
                $fieldsetFields[$field]['fieldset_order'] = $order;
			}
            $order++;
			$fields = am($fields,$fieldsetFields);
			
		}	
		if($entity == Fieldset::TYPE_REVIEW)
		{
			$fields = am($fields,$this->RateableField->getRatableFieldsAsCustomFields($id));
		}
		return $fields;
	}
	function refreshCustomFieldsCache($id)
	{
		cri("ListCategory")->refreshListingTypeCache($id);
	}
	
	function afterSave($created)
	{
		$this->refreshCustomFieldsCache($this->id);
		return parent::afterSave($created);
	}

}
?>